package cn.zkw.controller.back;

import cn.zkw.service.*;
import cn.zkw.util.action.AbstractAction;
import cn.zkw.util.split.SplitPageUtil;
import cn.zkw.vo.*;
import com.sun.corba.se.impl.protocol.NotLocalLocalCRDImpl;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
public class BackController extends AbstractAction {
    @Autowired
    SortService sortService;

    @Autowired
    ArticleService articleService;

    @Autowired
    VisitorVolumeService visitorVolumeService;

    @Autowired
    LabelService labelService;

    @Autowired
    NoticeService noticeService;

    @Autowired
    WordsService wordsService;

    /**
     * 获取访问量接口
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/volume", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody Object getVolume(HttpServletRequest request) throws Exception {
        JSONObject jsonObject = new JSONObject();
        List<VisitorVolume> list = visitorVolumeService.findAllVisitorVolume(); //获取全部访问量
        String day[] = new String[list.size()];
        Integer volume[] = new Integer[list.size()];
        Iterator<VisitorVolume> iterator = list.iterator();
        for (int i=0;i<list.size();i++){           //遍历list集合
            VisitorVolume vo = (VisitorVolume)list.get(i);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM.dd");
            day[i] = simpleDateFormat.format(vo.getDay());//添加日期
            volume[i] = vo.getVolume();
        }
        jsonObject.put("day",day);
        jsonObject.put("volume",volume);
        return jsonObject;
    }

    //增加公告页面跳转
    @RequestMapping("/back/addNotice")
    public String addNotice(HttpServletRequest request){
        request.setAttribute("li_number","2");
        SimpleDateFormat format = new SimpleDateFormat();
        String date = format.format(new Date());
        request.setAttribute("date",date);
        return "/back/add_notice";
    }

    //后台主页
    @RequestMapping("/back/main")
    public String main(HttpServletRequest request){
        Integer volumeNum = visitorVolumeService.selectAll();  //查询总访问量
        request.setAttribute("volume",volumeNum);
        request.setAttribute("li_number",0);
        return "/back/main";
    }

    //后台公告页面跳转
    @RequestMapping("/back/notice")
    public String notice(HttpServletRequest request){
        Words words =  wordsService.getWords();  //获取每日一句
        request.setAttribute("li_number",2);
        List<Notice> list = noticeService.selectAll();
        request.setAttribute("noticeList",list);
        request.setAttribute("noticeNumber",list.size());
        request.setAttribute("words",words);
        return "/back/notice";
    }

    //公告修改页面跳转
    @RequestMapping("/back/updateNotice")
    public String updateNotice(HttpServletRequest request,Integer notice_id){
        request.getSession().setAttribute("li_number",2);
        Notice notice = noticeService.selectById(notice_id);    //查找
        request.setAttribute("notice",notice);
        return "/back/update_notice";
    }

    //文章添加的页面跳转,加载了文章类别的数据
    @RequestMapping("/back/add_article")
    public ModelAndView addArticle(){
        //获取当前日期,显示在添加文章页面
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String datestr = simpleDateFormat.format(date);

        ModelAndView modelAndView = new ModelAndView("/back/add-article");
        modelAndView.addObject("date",datestr);
        modelAndView.addObject("sortList",sortService.findAllSort());  //传入文章类别集合
        return modelAndView;  //跳转到文章添加页面
    }

    //设置split分页参数，向页面传输
    public  void setPageSplitValue(String url, SplitPageUtil spu, Integer allRecorders,HttpServletRequest request) {
        request.setAttribute("url",url);
        request.setAttribute("cp", spu.getCurrentPage());
        request.setAttribute("ls", spu.getLineSize());
        request.setAttribute("col", spu.getColumn());
        request.setAttribute("wk", spu.getKeyWord());
        request.setAttribute("allRecorders", allRecorders);
    }

    //文章列表
    @RequestMapping("/back/article")
    public String article(HttpServletRequest request){
        //分页查询
        SplitPageUtil spu = new SplitPageUtil(request,"");
        int allRecorders = articleService.getArticleAllNum(); //文章总数
        Integer currentPage = spu.getCurrentPage();  //当前页
        Integer lineSize = spu.getLineSize();
        List<Article> articleList = articleService.splitArticleback((currentPage-1)*lineSize); //查询
        for(int i=0;i<articleList.size();i++){
            List<Label> labels = labelService.selectLabelByArticle_id(articleList.get(i).getArticle_id());  //获取文章标签
            StringBuffer stringBuffer = new StringBuffer();
            for (int j=0;j<labels.size();j++){
                stringBuffer.append(labels.get(j).getLabel_name()).append('丶');
            }
            String labelstr = "          ";
            if(stringBuffer.length()>0){    //如果没有标签则标签字符串为空
                labelstr = stringBuffer.substring(0,stringBuffer.length()-1);
            }
            articleList.get(i).setLabel_name(labelstr);  //将标签添加到文章列表
        }

        this.setPageSplitValue("/back/article",spu,allRecorders,request);
        request.setAttribute("articleList",articleList);
        request.setAttribute("li_number",1);   //后台界面的左菜单，需要知道当前请求的是什么功能
        return "/back/article";
    }

    /**
     * 跳转修改文章页面
     * @return
     */
    @RequestMapping("/back/updateArticle")
    public ModelAndView updateArticle(Integer article_id){
        Article article = articleService.selectArticleById(article_id);
        article.setArticle_id(article_id);
        //获取当前日期,显示在添加文章页面
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");  //发布日期
        String datestr = simpleDateFormat.format(article.getArticle_date());
        ModelAndView modelAndView = new ModelAndView("/back/update-article");  //页面跳转
        //获取文章标签，转换为，分隔的标签
        List<Label> labels = labelService.selectLabelByArticle_id(article.getArticle_id());
        StringBuffer stringBuffer = new StringBuffer();
        for (int j=0;j<labels.size();j++){
            stringBuffer.append(labels.get(j).getLabel_name()).append(',');
        }
        String labelstr = "";
        if(stringBuffer.length()>0){    //如果没有标签则标签字符串为空
            labelstr = stringBuffer.substring(0,stringBuffer.length()-1);
        }
        article.setLabel_name(labelstr);

        modelAndView.addObject("a",article);
        modelAndView.addObject("date",datestr);
        modelAndView.addObject("sortList",sortService.findAllSort());  //传入文章类别集合

        return modelAndView;
    }

    @Override
    public String getType() {
        return null;
    }

    @Override
    public String getFileUploadDir() {
        return null;
    }
}
